<template>
  <a-modal title="计量设备" :visible="dialogVisible" @ok="handleOk" @cancel="handleCancel"
           class="large-modal">
    <searchData :searchData="searchArr" @search="search"></searchData>
    <b-table ref="table" bordered :columns="columns" :request="tableData"
             :selectConfig="{ use: true, type: 'checkbox', rowKey: 'id' }"
             @select-click="hanldeClick">
      <span slot="num" slot-scope="text, record, index">{{ index + 1 }}</span>
      <template slot="actions" slot-scope="text, record">
        <a @click="detailsItem(record)">查看详情</a>
      </template>
    </b-table>
  </a-modal>
</template>
<script>
import { getList } from '@/api/quality/projectMeterageEquipmentAccount'
import dataFilter from '@/views/quality/equipmentManagement/constant/dataFilter'
import { equipmentStatusArr } from '@/views/quality/equipmentManagement/constant/dictionary'
export default {
  name: 'equipmentSelected',
  props: {
    form: Object,
    visible: {
      type: Boolean,
      required: false
    }
  },
  data() {
    return {
      selectedRows: {},
      queryParams: {},
      searchArr: [
        // 搜索框
        {
          name: '设备名称',
          type: 'input',
          value: 'mdInfoName',
          disabled: true
        },
        {
          name: '规格型号',
          type: 'input',
          value: 'mdModelName',
          disabled: true,
        },
        {
          name: '设备编号',
          type: 'input',
          value: 'mdCode'
        },
        {
          name: '进场时间',
          type: 'dateRange',
          value: 'inProjectTime',
        },
      ],
      columns: [
        {
          title: '序号',
          width: 85,
          scopedSlots: { customRender: 'num' }
        },
        {
          title: '设备名称',
          ellipsis: true,
          dataIndex: 'mdName',
        },
        {
          title: '设备编码',
          ellipsis: true,
          dataIndex: 'mdCode'
        },
        {
          title: '规格型号',
          ellipsis: true,
          dataIndex: 'specificationModel',
        },
        {
          title: '进场时间',
          ellipsis: true,
          dataIndex: 'inProjectTime',
        },
        {
          title: '使用单位',
          ellipsis: true,
          dataIndex: 'projectName',
        },
        {
          title: '设备状态',
          ellipsis: true,
          dataIndex: 'mdStatus',
          customRender: (text) => {
            return dataFilter(equipmentStatusArr, ['1']).find(item => item.value === text)?.label
          }
        },
        {
          title: '操作',
          dataIndex: 'actions',
          width: '230px',
          fixed: 'right',
          scopedSlots: { customRender: 'actions' }
        }
      ],
      tableData: (parameter) => {
        let { pageNo, pageSize } = parameter;
        let params = {
          pageNum: pageNo,
          pageSize
        };
        const requestParameters = Object.assign({}, params, { param: this.setQueryParam() })
        return getList(requestParameters).then((res) => {
          if (res.code === '0') {
            return { code: 200, data: res.data?.list, totalSize: res.data?.total }
          }
        })
      }
    }
  },
  watch: {
    visible: {
      handler(val) {
        this.dialogVisible = val
      },
      immediate: true
    },
  },
  methods: {
    /**
     * @description: 设置查询参数
     */
    setQueryParam() {
      if (this.queryParams.inProjectTime && this.queryParams.inProjectTime.length > 0) {
        this.queryParams.inProjectStartTime = this.queryParams.inProjectTime[0].format('YYYY-MM-DD') + ' 00:00:00';
        this.queryParams.inProjectEndTime = this.queryParams.inProjectTime[1].format('YYYY-MM-DD') + ' 23:59:59';
        delete this.queryParams.inProjectTime;
      }
      const searchQuery = { orgSet: [], detSet: [], projectSet: [] };
      if (this.form.projectId) {
        searchQuery.projectSet = this.form.projectId ? [this.form.projectId] : []
      }
      this.queryParams.searchQuery = searchQuery
      this.queryParams.mdBelongTo = 'OWN',
        this.queryParams.mdStatus = '1'
      return this.queryParams
    },
    /**
     * @description: 单选项目
     * @param {Object} data 选中行数据
     **/
    hanldeClick(data) {
      this.selectedRows = data
    },
    /**
     * @description: 操作关闭
     **/
    handleCancel(e) {
      this.$emit('handelShow', false)
      this.selectedRows = {}
    },
    /**
     * @description: 操作确定
     **/
    handleOk: function () {
      this.$emit('handleClick', this.selectedRows)
      this.$emit('handelShow', false)
      this.selectedRows = {}
    },
    /**
     * @description: 查询
     * @param {Object} val 筛选项
     **/
    search(val) {
      this.queryParams = val || {}
      this.$refs.table.getData(null, true)
    },
    /**
     * @description: 查看详情
     * @param {Object} record 当前行数据
     **/
    detailsItem(record) {
      this.handleCancel();
      this.$router.push({
        path: '/quality/equipmentManagement/projectMeterageEquipmentAccountInfo',
        query: {
          editModel: 0,
          mdOwnId: record.mdOwnId,
          id: record.id,
          projectId: record.projectId,
        }
      })
    }
  }
}
</script>
